<%@ page contentType="text/html;charset=UTF-8"%>
<%@ page language="java" import="java.util.*"%>
<%@ page import="com.ejoysoft.wlanyz.business.Latlng,com.ejoysoft.common.Constants,com.ejoysoft.util.ParamUtil"%>
<%@page import="javax.script.ScriptEngineManager"%>
<%@page import="javax.script.ScriptEngine"%>
<%@page import="java.math.BigDecimal"%>
<%@page import="com.sun.org.apache.bcel.internal.generic.NEW"%>
<%@page import="com.ejoysoft.common.exception.IdObjectException"%>
<%@ include file="../include/jsp/head.jsp"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
  String  strId = ParamUtil.getString(request,"strId",""); 
  if(strId.equals(""))
  	 throw new IdObjectException("请求处理的信息id为空！或者已经不存在");
   String where = "where strId = '"+strId+"'";
   Latlng obj=new Latlng(globa,false);
   Latlng obj0=obj.show(where);
  
    if(obj0==null){
        globa.closeCon();
        throw new IdObjectException("请求处理的信息id='"+strId+"'对象为空！","请检查该信息的相关信息");
    }
 
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html>
<head>
<link href="../common/css/css.css" rel="stylesheet" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><style type="text/css">
<!-- 
body {
	background-color: #FFFFFF; font-size:12px;
}
.sel1{ width:160px;}
.t2{font-size:14px; color:#000000}
.t1{font-size:12px;}
.t21 {font-size:14px; color:#000000}

.mesWindow{border:#666 0px solid;background:#fff}
.mesWindowTop{border:#eee 0px solid;margin-left:0px;padding:0px;font-weight:bold;text-align:left;font-size:12px;}
.mesWindowContent{margin:0px;font-size:12px;}
.mesWindow .close{height:15px;width:28px;border:none;cursor:pointer;text-decoration:underline;background:#fff}
-->
</style> 
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">

var EARTH_RADIUS = 6378137.0;    //单位M
    var PI = Math.PI;
    
    function getRad(d){
        return d*PI/180.0;
    }
    
    /**
     * caculate the great circle distance
     * @param {Object} lat1
     * @param {Object} lng1
     * @param {Object} lat2
     * @param {Object} lng2
     */
    function getGreatCircleDistance(lat1,lng1,lat2,lng2){
        var radLat1 = getRad(lat1);
        var radLat2 = getRad(lat2);
               
        var a = radLat1 - radLat2;
        var b = getRad(lng1) - getRad(lng2);
        
        var s = 2*Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) + Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
        s = s*EARTH_RADIUS;
        s = Math.round(s*10000)/10000.0;  
        return s;
    }


   function latlngdistance(lat2, lng2){
      var strlatlng1 = document.getElementById('originalLocation').value ;
      //数据库中originalLocation数据没有值，默认为null，故当系统中没有经纬度时，做如下判断
      if(strlatlng1 == "null" )return 0;
      else{
	      var strLatlng1 = strlatlng1.split(",",2);     
	      var lat1 = parseFloat(strLatlng1[0]);
	      var lng1 = parseFloat(strLatlng1[1]);        
	      var distance = getGreatCircleDistance(lat1,lng1,lat2,lng2);
	      return distance;
      }
   }

//绘图并标记
    var geocoder;
    var geocoderLatlng;
    var map;
    var markerArray =[];//保存地址解析过程中的标记 
    var image = '../images/map_blue.png';
     
   var screenZoom  = 12;   
   if(screen.height > 1000) screenZoom = 14;//依据屏幕高度设置缩放比例 
    
    //初始化google地图
    function initialize() {
        geocoder = new google.maps.Geocoder();
        geocoderLatlng = new google.maps.Geocoder();
        var latlng = new google.maps.LatLng(32.39421, 119.41296599999998);
        var myOptions = {
            zoom: screenZoom,//缩放比例
            center: latlng,//中心经纬度 
            mapTypeId: google.maps.MapTypeId.ROADMAP //地图类型 ?MapTypeId.ROADMAP ?MapTypeId.SATELLITE ?MapTypeId.HYBRID ?MapTypeId.TERRAIN 
    
        }      
        map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    }

    //添加点击标记监听事件
    function addListener(pointMarker , infowindow , map,flag,dis,checkedLocation,acName,acLocation,acLocationedAddr,lng,lat){
     google.maps.event.addListener(pointMarker, 'click', function (){
     //  infowindow.open(map, pointMarker);
       testMessageBox(event,flag,dis,checkedLocation,acName,acLocation,acLocationedAddr,lng,lat);
     });
    }
    


   
   //地址解析到纬度并标记到地图上
    function showAddress(address1, address2,tag) {  
    
      var address;
      var acName = address1;
      var  acLocation = address2;
      if(tag == 1){
       address = address2; //按热点地址解析 
      }
      else{
       address = address1;   //按热点名称解析  
      }
    
    
      for(i=0; i<markerArray.length; i++){
       //先清除地图上的marker
       	  markerArray[i].setMap(null);
      }   
      markerArray = [];  
     //热点解析
       geocoder.geocode({'address': address }, function (results, status) { //地址解析
            if (status == google.maps.GeocoderStatus.OK) {
             map.setCenter(results[0].geometry.location);//将解析到的第一经度纬度设置wei居中坐标
            map.setZoom(screenZoom);
            for(i=0; i<results.length; i++){   
              
                var marker = new google.maps.Marker({
                    map: map,
                    position: results[i].geometry.location,
                    title:results[i].formatted_address,
                    animation: google.maps.Animation.DROP //坐标动画效果
                });
              
               var lat = results[i].geometry.location.lat().toFixed(5);
               var lng = results[i].geometry.location.lng().toFixed(5);//保留经纬度后5位             
               var checkedLocation = lat + ',' + lng;
               var dis = latlngdistance(lat,lng);//计算经纬度之间的距离 
                markerArray[i] = marker; 
                addListener(markerArray[i], infowindow , map,0,dis,checkedLocation,acName,acLocation,results[i].formatted_address,lng,lat);//因为注册监听器是异步的，故必须把注册监听器的操作独立放进一个函数中，否则，经纬度将会永远是最新的同一个值
              }   
            } else {
              if(tag == 2 ){//默认加载时，如果热点名称解析失败，则解析热点地址 
			               geocoder.geocode({ 'address': acLocation }, function (results, status) { //地址解析
				            if (status == google.maps.GeocoderStatus.OK) {
				            map.setCenter(results[0].geometry.location);//依据解析的经度纬度设置坐标居中
				            map.setZoom(screenZoom);
				            for(i=0; i<results.length; i++){   
				              
				                var marker = new google.maps.Marker({
				                    map: map,
				                    position: results[i].geometry.location,
				                    title:results[i].formatted_address,
				                    animation: google.maps.Animation.DROP //坐标动画效果
				                });
				              
				               var lat = results[i].geometry.location.lat().toFixed(5);
				               var lng = results[i].geometry.location.lng().toFixed(5);//保留经纬度后5位             
				               var  checkedLocation = lat + ',' + lng;
				               var   dis = latlngdistance(lat,lng);				               
				                markerArray[i] = marker; 
				                addListener(markerArray[i], infowindow , map,1,dis,checkedLocation,acName,acLocation,results[i].formatted_address,lng,lat);//因为注册监听器是异步的，故必须把注册监听器的操作独立放进一个函数中，否则，经纬度将会永远是最新的同一个值
				    	              }   
			            }       
			        });
			        }
            }
           
        });    

  }
  //提交表单审核经纬度
  function checkLatLng(distance,strId,location)
  {
  		document.getElementById("distance").value = distance;
  		document.getElementById("strId").value = strId;
  		document.getElementById("checkedLocation").value = location;
  		document.getElementById("frm").submit();
  		
  }
  
  //反向地址解析，经纬度解析成地址并以自定义的图标标记
  function showLatlng(acName,acLocation,strlatlng,strlatlng2) {  
   
      var strLatlng = strlatlng.split(",",2);
      var lat = parseFloat(strLatlng[0]);
      var lng = parseFloat(strLatlng[1]);
      var latlng = new google.maps.LatLng(lat,lng);
    
      map.setZoom(screenZoom);
      geocoder.geocode({'latLng': latlng}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
      	map.setCenter(latlng);
        if (results[0]) {
          var  markerOriginal = new google.maps.Marker({
              position: latlng,
              map: map,
              icon:image,
              title:results[0].formatted_address,
              animation: google.maps.Animation.DROP //坐标动画效果
          });
         
            addListener(markerOriginal, infowindow , map,2,'0','',acName,acLocation,results[0].formatted_address,lng,lat);
        }
      } 
    });   
    
    
     if(strlatlng2 != ""){
	     var  strLatlng2 = strlatlng2.split(",",2);
	     var  lat2 = parseFloat(strLatlng2[0]);
	     var  lng2 = parseFloat(strLatlng2[1]);
	     var  latlng2 = new google.maps.LatLng(lat2,lng2);
      
      geocoder.geocode({'latLng': latlng2}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
      	map.setCenter(latlng2);
        if (results[0]) {
          var  markerChecked = new google.maps.Marker({
              position: latlng2,
              map: map,
              title:results[0].formatted_address,
              animation: google.maps.Animation.DROP //坐标动画效果
          });
            addListener(markerChecked, infowindow , map,3,'0','',acName,acLocation,results[0].formatted_address,lng2,lat2);
        }
      } 
    });
    }
    
    if(strlatlng == "null" && strlatlng2 == "null")initialize();
  }
  
  function show(tag){
    //tag=0时只解析热点名称；，tag=1时只解析热点地址；tag=2时，如果热点名称解析失败则解析热点地址
    var acName = document.getElementById('acName').value;    
    var acLocation = document.getElementById('acLocation').value;
    var originalLocation = document.getElementById('originalLocation').value; 
    var checkedLocation = document.getElementById('checkedLocation').value; 
	  showLatlng(acName,acLocation,originalLocation,checkedLocation);  
  }
  
</script>

<script>
var isIe=(document.all)?true:false;
//设置select的可见状态
function setSelectState(state)
{
var objl=document.getElementsByTagName('select');
for(var i=0;i<objl.length;i++)
{
objl[i].style.visibility=state;
}
}
function mousePosition(ev)
{
if(ev.pageX || ev.pageY)
{
return {x:ev.pageX, y:ev.pageY};
}
return {
x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,y:ev.clientY + document.body.scrollTop - document.body.clientTop
};
}
//弹出方法
function showMessageBox(pos,wWidth,wHeight,flag,dis,checkedLocation,acName,acLocation,acLocationedAddr,lng,lat)
{
closeWindow();
var bWidth=parseInt(document.documentElement.scrollWidth);
var bHeight=parseInt(document.documentElement.scrollHeight);
if(isIe){
setSelectState('hidden');}
var back=document.createElement("div");
back.id="back";
var styleStr="top:0px;left:0px;position:absolute;background:#666;width:"+bWidth+"px;height:"+bHeight+"px;";
styleStr+=(isIe)?"filter:alpha(opacity=0);":"opacity:0;";
back.style.cssText=styleStr;
document.body.appendChild(back);
showBgSuddenlly(back,50);//背景突然变暗
//showBgGradually(back,50);//背景逐渐变暗
var mesW=document.createElement("div");
mesW.id="mesWindow";
mesW.className="mesWindow";  
var tempString =  "<div id='contentDiv' class='mesWindowTop'><table width='100%' border='0' align='center' cellpadding='0' cellspacing='0' bgcolor='8FA6BD' >"+
                                    "<tr> <td height='32' valign='middle' background='../images/ri_bg1.jpg' class='t21'><table width='100%' height='32' border='0' cellpadding='0' cellspacing='0'><tr>";
var latlngString = "";
if(flag == 0)
{
	tempString = tempString+"<td>&nbsp;&nbsp;<strong><font style='font-size:14px' color='#FFFFFF'>解析位置<font> </strong></td>" + 
                            "<td style='padding:0px;margin:0px' align='right' valign='bottom'><img src='../images/sc.jpg' onclick='checkLatLng(&quot;"+dis+"&quot;,&quot;"+<%=strId%>+"&quot;,&quot;"+checkedLocation+"&quot;);' style='cursor:hand' /></td>"+
                           	"<td style='padding:0px;margin:0px;width:52px ' valign='bottom' align='right'><img src='../images/clw.jpg' onclick='closeWindow();' title='关闭窗口'  style='cursor:hand'/></td>";	
	latlngString = "解析经纬度";
}else if(flag == 1)
{
	tempString  =tempString+  "<td>&nbsp;&nbsp;<strong><font style='font-size:14px' color='#FFFFFF'>解析位置 </font></strong></td>" + 
		                     "<td style='padding:0px;margin:0px' valign='bottom' align='right'><img src='../images/sc.jpg' onclick='checkLatLng(&quot;"+dis+"&quot;,&quot;"+<%=strId%>+"&quot;,&quot;"+checkedLocation+"&quot;);' style='cursor:hand' /></td>"+
		                      "<td style='padding:0px;margin:0px;width:52px ' valign='bottom' align='right'><img src='../images/clw.jpg' onclick='closeWindow();' title='关闭窗口'  style='cursor:hand' /></td>";
	 latlngString = "解析经纬度";
}else if(flag == 2)
{
	tempString  = tempString+ "<td>&nbsp;&nbsp;<strong><font style='font-size:14px' color='#FFFFFF'>系统位置</font> </strong></td>" + 
				"<td style='padding:0px;margin:0px;width:52px ' valign='bottom' align='right'><img src='../images/clw.jpg' onclick='closeWindow();' title='关闭窗口'  style='cursor:hand'/></td>";
   latlngString = "系统经纬度";
}else if(flag == 3)
{
	tempString  = tempString+ "<td>&nbsp;&nbsp;<strong><font style='font-size:14px' color='#FFFFFF'>解析位置 </font></strong></td>" + 
						                             	"<td style='padding:0px;margin:0px;width:52px ' valign='bottom' align='right'><img src='../images/clw.jpg' onclick='closeWindow();' title='关闭窗口'  style='cursor:hand' /></td>";
	latlngString = "系统经纬度";
}
tempString = tempString + "</tr></table></td></tr><tr> <td background='../images/tdbg.jpg' bgcolor='#FFFFFF'><table width='100%' border='0' align='center' cellpadding='0' cellspacing='0'>"+
						            "<tr> <td width='27%' style='margin-left:5px;padding-left:10px' height='35' class='t1'><strong>热          点： </strong>"+ acName + "</td></tr>" + 
						            "<tr> <td width='27%' style='margin-left:5px;padding-left:10px' height='35' class='t1'><strong>热点地址： </strong>" + acLocation + "</td></tr>" + 
						            "<tr> <td width='27%' style='margin-left:5px;padding-left:10px' height='35' class='t1'><strong>解析地址： </strong>" + acLocationedAddr + "</td></tr>" +
						            "<tr> <td width='27%' style='margin-left:5px;padding-left:10px' height='35' class='t1'><strong>"+latlngString+"：（" + lng + "，" + lat +  "）</td></tr>"  +
						            "</table></td></tr></table></td></tr></table> </div>";
mesW.innerHTML=tempString; 
//styleStr="left:"+(((pos.x-wWidth)>0)?(pos.x-wWidth):pos.x)+"px;top:"+(pos.y)+"px;position:absolute;width:"+wWidth+"px;";
styleStr="";
var leftPoint = pos.x;
var topPoint = pos.y;
var flagWP = 0;//横坐标左对齐
var flagHP = 0;//纵坐标上对齐
if((pos.x + wWidth) > bWidth){
	leftPoint = pos.x - wWidth;
	flagWP = 1;//横坐标右对齐
}
if(pos.y + wHeight > bHeight){
	topPoint = pos.y - wHeight;
	flagHP = 1;//纵坐标下对齐
}
styleStr="left:"+leftPoint+"px;top:"+topPoint+"px;position:absolute;width:"+wWidth+"px;";
mesW.style.cssText=styleStr;
document.body.appendChild(mesW);
}
//让背景渐渐变暗
function showBgGradually(obj,endInt)
{
if(isIe)
{
	obj.filters.alpha.opacity+=1;
	if(obj.filters.alpha.opacity<endInt)
	{
		setTimeout(function(){showBgGradually(obj,endInt)},5);
	}
}else{
	var al=parseFloat(obj.style.opacity);
	al+=0.01;
	obj.style.opacity=al;
	if(al<(endInt/100))
	{
		setTimeout(function(){showBgGradually(obj,endInt)},5);}
	}
}

//让背景突然变暗
function showBgSuddenlly(obj,endInt)
{
	if(isIe)
	{
		obj.filters.alpha.opacity += endInt;//背景突然变暗
	}else{
		var al=parseFloat(obj.style.opacity);
		al+=0.01*endInt;
	 	obj.style.opacity=al;//背景突然变暗
		 
	}
}
//关闭窗口
function closeWindow()
{
if(document.getElementById('back')!=null)
{
document.getElementById('back').parentNode.removeChild(document.getElementById('back'));
}
if(document.getElementById('mesWindow')!=null)
{
document.getElementById('mesWindow').parentNode.removeChild(document.getElementById('mesWindow'));
}
if(isIe){
	setSelectState('');}
}
//弹出窗口
function testMessageBox(ev,flag,dis,checkedLocation,acName,acLocation,acLocationedAddr,lng,lat)
{
	var objPos = mousePosition(ev); 
	showMessageBox(objPos,400,200,flag,dis,checkedLocation,acName,acLocation,acLocationedAddr,lng,lat);
}
</script>
<title>WILAN优化分析系统</title>
 </head>
  <form id=frm name=frm action="show_map.jsp" method="post">
 <input type="hidden" name="strId" id="strId" value=""/>
 <input type="hidden" name="distance" id="distance" value=""/>
 <input type="hidden" name="actionflag" id="actionflag" value="checkedLatLng"/>
 <input type="hidden" name="checkedLocation" id="checkedLocation" value="<%=obj0.getCheckedLocation()%>"/>
 </form>
 <body onload="initialize();show(2);"> 

<table width="100%" height="100%" align="center" border="0" cellpadding="0" cellspacing="0" bgcolor="8FA6BD" >   
 
<tr><td width="100%" bgcolor="#E9F1FE">
  </td></tr>
   <tr height="100%">
  	     <td>   <div id="map_canvas" style="width: 100%; height: 100%"></div></td>
   </tr >
<tr><td bgcolor="#E9F1FE">
<table align="center" >
   <tr> 
	     <td ><img src = "../images/jxwd.jpg"  ></img>热点位置</td>
   </tr>
</table></td></tr>
</table>
</body>
</html>
<%@ include file="../include/jsp/footer.jsp"%>
